const posting = require("../models/posting")
const replyPosting = require("../models/replyPosting")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
  var form = new formidable.IncomingForm();
  form.parse(req, function (err, fields, files) {
    if (err) {
        res.json({ 'state': false })
    } else {
      if(fields.postingId !== '') {
        console.log(fields.index === null)
        posting.deleteOne({
          _id: fields.postingId
        }, (err, doc) => {
          if (err) {
            res.json({ 'state': false, 'msg': err });
          } else {
            res.json({ 'state': true, 'msg': "ok" });
          }
        })
      } else {
        replyPosting.deleteOne({
          _id: fields.replyId
        }, (err, doc) => {
          if (err) {
            res.json({ 'state': false, 'msg': err });
          } else {
            res.json({ 'state': true, 'msg': "ok" });
          }
        })
      }
    }
  });
});

module.exports = router;
